<?php

/**
 * Salarie form base class.
 *
 * @method Salarie getObject() Returns the current form's model object
 *
 * @package    sop
 * @subpackage form
 * @author     Your name here
 */
abstract class BaseSalarieForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'guard_id'                              => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => false)),
      'nom'                                   => new sfWidgetFormInputText(),
      'prenom'                                => new sfWidgetFormInputText(),
      'date_embauche'                         => new sfWidgetFormDate(),
      'nb_heures_semaine'                     => new sfWidgetFormInputText(),
      'actif'                                 => new sfWidgetFormInputText(),
      'id'                                    => new sfWidgetFormInputHidden(),
      'asso_salarie_unite_fonctionnelle_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'UniteFonctionnelle')),
    ));

    $this->setValidators(array(
      'guard_id'                              => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id')),
      'nom'                                   => new sfValidatorString(array('max_length' => 45, 'required' => false)),
      'prenom'                                => new sfValidatorString(array('max_length' => 45, 'required' => false)),
      'date_embauche'                         => new sfValidatorDate(array('required' => false)),
      'nb_heures_semaine'                     => new sfValidatorNumber(),
      'actif'                                 => new sfValidatorInteger(array('min' => -128, 'max' => 127)),
      'id'                                    => new sfValidatorChoice(array('choices' => array($this->getObject()->getId()), 'empty_value' => $this->getObject()->getId(), 'required' => false)),
      'asso_salarie_unite_fonctionnelle_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'UniteFonctionnelle', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('salarie[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Salarie';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['asso_salarie_unite_fonctionnelle_list']))
    {
      $values = array();
      foreach ($this->object->getAssoSalarieUniteFonctionnelles() as $obj)
      {
        $values[] = $obj->getUfId();
      }

      $this->setDefault('asso_salarie_unite_fonctionnelle_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveAssoSalarieUniteFonctionnelleList($con);
  }

  public function saveAssoSalarieUniteFonctionnelleList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['asso_salarie_unite_fonctionnelle_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(AssoSalarieUniteFonctionnellePeer::SALARIE_ID, $this->object->getPrimaryKey());
    AssoSalarieUniteFonctionnellePeer::doDelete($c, $con);

    $values = $this->getValue('asso_salarie_unite_fonctionnelle_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new AssoSalarieUniteFonctionnelle();
        $obj->setSalarieId($this->object->getPrimaryKey());
        $obj->setUfId($value);
        $obj->save();
      }
    }
  }

}
